/**
 * @author zhounan 
 * @date 2024年5月20日
 * @version V1.0
 */
package org.zn.etl.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.zn.etl.config.Constant;
import org.zn.etl.job.context.GlobalContextKey;
import org.zn.etl.job.context.JobExecInstanceContext;
import org.zn.etl.job.entity.po.JobExecInstancePo;
import org.zn.etl.service.EtlExecuteService;

/**
 * @author zhounan 
 * @date 2024年5月20日
 * @version V1.0
 */
@RestController
@RequestMapping("etl/exec")
public class EtlExecuteController {
	
	@Autowired
	private EtlExecuteService etlExecuteService;
	
	@Autowired
	private DiscoveryClient discoveryClient;
	@Autowired
	private	RestTemplate restTemplate;

	/**
	 * 任务执行
	 * @param jobName
	 * @param execParam
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("job/{name}")
	public Map<String, Object> executeJob(@PathVariable("name") String jobName , @RequestBody Map<String, Object> execParam) throws Exception {
		
		Map<String, Object> resMap=new HashMap<>();
		String node=(String) execParam.get(GlobalContextKey.EXENODE);
		
		String host= InetAddress.getLocalHost().getHostAddress();
		
		if(StringUtils.isNotEmpty(node)) {

			if(!host.equals(node)) {
			
				List<ServiceInstance>  slist=discoveryClient.getInstances(Constant.SERVICE_NAME);
				
				for (ServiceInstance si : slist) {
				
					if(si.getHost().equals(node)) {
						String path=si.getUri()+"/etl/exec/job/"+jobName;
					
						resMap =restTemplate.postForEntity(path, execParam, Map.class).getBody();
				
						return resMap;
					}
				
				}
			}
		}
		
		JobExecInstanceContext  ins=etlExecuteService.executeJob(jobName, execParam);
		
		resMap.put("instanceId", ins.getId());
		resMap.put("status", ins.getStatus());
		resMap.put(GlobalContextKey.EXENODE, host);

		return resMap;

	}
	
	/**
	 * 任务组执行
	 * @param groupName
	 * @param execParam
	 * @return
	 * @throws UnknownHostException 
	 */
	@PostMapping("group/{name}")
	public Map<String, Object> executeJobGroup(@PathVariable("name") String groupName , @RequestBody Map<String, Object> execParam) throws UnknownHostException {
		
		Map<String, Object> resMap=new HashMap<>();
		String node=(String) execParam.get(GlobalContextKey.EXENODE);
		
		String host= InetAddress.getLocalHost().getHostAddress();
		
		if(StringUtils.isNotEmpty(node)) {

			if(!host.equals(node)) {
			
				List<ServiceInstance>  slist=discoveryClient.getInstances(Constant.SERVICE_NAME);
				
				for (ServiceInstance si : slist) {
				
					if(si.getHost().equals(node)) {
						
						String path=si.getUri()+"/etl/exec/group/"+groupName;
					
						resMap =restTemplate.postForEntity(path, execParam, Map.class).getBody();
				
						return resMap;
					}
				
				}
			}
		}
		
		resMap.put(GlobalContextKey.EXENODE, host);
		resMap.put("status", "running");
		etlExecuteService.executeGroup(groupName, execParam);
		
		return resMap;

	}
	
	/**
	 * 查询全局 参数
	 * @return
	 */
	@GetMapping("context/global")
	public Map<String, Object> getGlobalContext() {
		
		return etlExecuteService.getGlobalContext();
	}
	
	/**
	 * 查询执行结果
	 * @return
	 */
	@GetMapping("/job/ins")
	public List<JobExecInstancePo> getJobexecIns(Long jobId, String startTime, String endTime, String status) {
		
		return etlExecuteService.queryInstanceList(jobId, startTime, endTime, status);
	}
	
	
	
	
	
	
}
